package com.linkage.usercenter.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.linkage.common.exception.ServiceException;
import com.linkage.usercenter.dao.CoreDptInfoMapper;
import com.linkage.usercenter.dao.CoreUserMapper;
import com.linkage.usercenter.dao.MeetingInfoMapper;
import com.linkage.usercenter.pojo.entity.CoreDptInfo;
import com.linkage.usercenter.pojo.entity.CoreUser;
import com.linkage.usercenter.pojo.entity.MeetingInfo;
import com.linkage.usercenter.pojo.vo.DptInfoLevelVO;
import com.linkage.usercenter.pojo.vo.DptUserVo;
import com.linkage.usercenter.pojo.vo.UserInfoVO;
import com.linkage.usercenter.pojo.vo.UserMgInfoVO;
import com.linkage.usercenter.service.IManageService;
import com.linkage.usercenter.service.MeetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MeetingServiceImpl implements MeetingService {

    @Autowired
    private MeetingInfoMapper meetingInfoMapper;

    @Autowired
    private CoreDptInfoMapper coreDptInfoMapper;

    @Autowired
    private CoreUserMapper userMapper;

    @Override
    public List<MeetingInfo> queryMeetingList(String parentDptCode) {
        List<MeetingInfo> meetingInfoList = new ArrayList<>();
        List<MeetingInfo> self = meetingInfoMapper.queryList(MeetingInfo.builder().dptCode(parentDptCode).build());
        if(ObjectUtil.isNotEmpty(self)){
            meetingInfoList.addAll(self);
        }
        List<CoreDptInfo> dptInfoList = getDptList(parentDptCode);
        dptInfoList.forEach(dpt -> {
            List<MeetingInfo> list = meetingInfoMapper.queryList(MeetingInfo.builder().dptCode(dpt.getDptCode()).build());
            if(ObjectUtil.isNotEmpty(list)){
                meetingInfoList.addAll(list);
            }
        });
        meetingInfoList.forEach(meeting -> {
            List<Map> userInfoVOList = new ArrayList<>();
            String[] userCodes = meeting.getUserids().split(",");
            if(userCodes != null && userCodes.length>0){
                for(String userCode : userCodes){
                    CoreUser user = userMapper.selectByUserCode(userCode);
                    userInfoVOList.add(MapUtil.builder().put("userCode",user.getUserCode()).put("userName",user.getUserRealName()).build());
                }
                meeting.setUserList(userInfoVOList);
            }
        });
        return meetingInfoList;
    }

    private List<CoreDptInfo> getDptList(String dptCode){
        List<CoreDptInfo> result = new ArrayList<>();
        List<CoreDptInfo> list = coreDptInfoMapper.queryByParentCode(dptCode);
        if(ObjectUtil.isNotEmpty(list)){
            result.addAll(list);
            for(CoreDptInfo dptInfo : list){
                result.addAll(getDptList(dptInfo.getDptCode()));
            }
        }
        return result;
    }

    @Override
    public MeetingInfo queyMeetingInfo(String meetingId) {
        return meetingInfoMapper.selectByPrimaryKey(meetingId);
    }

    @Override
    public void updateMeeting(MeetingInfo meetingInfo) {
        if(StrUtil.isEmpty(meetingInfo.getMeetingId())){
            throw new ServiceException(500,"会议ID参数为空");
        }
        meetingInfoMapper.updateByPrimaryKeySelective(meetingInfo);
    }

    @Override
    public void addMeeting(MeetingInfo meetingInfo) {
        if(StrUtil.equals("0",meetingInfo.getFlag()) && StrUtil.isEmpty(meetingInfo.getDptCode())){
            throw new ServiceException(500,"添加会议部门code不能为空！");
        }
        meetingInfo.setMeetingId(IdUtil.fastSimpleUUID());
        meetingInfo.setCode(IdUtil.fastSimpleUUID());
        meetingInfoMapper.insertSelective(meetingInfo);
    }

    @Override
    public void deleteMeeting(String meetingId) {
        if(StrUtil.isEmpty(meetingId)){
            throw new ServiceException(500,"会议ID参数为空");
        }
        meetingInfoMapper.deleteByPrimaryKey(meetingId);
    }

    @Override
    public void deleteMeetingByDptCode(String dptCode) {
        if(StrUtil.isEmpty(dptCode)){
            throw new ServiceException(500,"部门code参数为空");
        }
        meetingInfoMapper.deleteByDptCode(dptCode);
    }
}
